<template>
  <!--添加分享卡位 -->
  <el-dialog title="新增分享" :visible.sync="dialogVisible" :close-on-click-modal="false" width="650px" append-to-body>
    <div class="form-box">
      <el-form ref="form" :model="form" :rules="rules" label-width="110px" class="demo-ruleForm">

        <el-form-item label="卡位手机号" prop="tel">
          <span>{{form.tel}}</span>
        </el-form-item>
        <el-form-item label="运营商" prop="gameCarrierIdsArr">
          <el-select multiple v-model="form.gameCarrierIdsArr" placeholder="请选择游戏游戏运营商" @change="carrierChange">
            <el-option v-for="dict in yunyingshangList" :key="dict.id" :label="dict.name" :value="dict.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="游戏账号" prop="pwd">
          <el-input v-model="form.pwd" placeholder="请输入游戏账号" />
        </el-form-item>
        <el-form-item label="短信显示数量" prop="num">
          <el-input v-model="form.num" placeholder="请输入短信显示数量" />
        </el-form-item>
        <el-form-item label="到期时间" prop="expireTime">
          <el-date-picker style="width:250px" v-model="form.expireTime" type="datetime" placeholder="请选择到期时间" format="yyyy-MM-dd HH:mm:ss" value-format="yyyy-MM-dd HH:mm:ss">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="短信过滤" prop="guolv">
          <el-input v-model="form.guolv" type="textarea" placeholder="请输入关键字，包含关键字的短信将会被过滤掉" :rows="2" style="width:400px" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请填写备注" :rows="4" style="width:400px" />
        </el-form-item>
        <div class="tip">
          提示：分享链接后，任何人打开链接都能看到该手机号接收的短信，请注意账号安全！
        </div>

        <div class="center">
          <el-button @click="close()">取消</el-button>
          <el-button type="primary" @click="submitForm('form')" v-loading="loading">提交</el-button>
        </div>
      </el-form>
    </div>
  </el-dialog>
</template>

<script>
import phoneApi from "@/api/phoneManage";
export default {
  name: "addFina",
  data() {
    var validatePass = (rule, value, callback) => {
      if (value && value.length != 11) {
        callback(new Error("请输入11位手机号"));
      } else {
        callback();
      }
    };
    return {
      dialogVisible: false,
      loading: false,
      yunyingshangList: [],

      // 表单参数
      form: {
        pwd: "",
        num: "",
        tel: "",
        gameCarrierIds: "",
        gameCarrierNames: "",
        expireTime: "",
        tenantId: "",
        tenantName: "",
        remark: "",
        gameCarrierIdsArr: [],
      },
      Code_url: "",
      rules: {
        num: [{ required: true, message: "请输入短信数量", trigger: "blur" }],
        expireTime: [
          { required: true, message: "请输入到期时间", trigger: "change" },
        ],
        gameCarrierIdsArr:[
        { required: true, message: "请选择", trigger: "change" },
        ]
      },
    };
  },
  created() {
  },
  computed: {
    shopInfo() {
      return this.$store.getters.shopInfo;
    },
    retailDomain() {
      return this.$store.getters.retailDomain;
    },
  },
  methods: {
    // 打开弹窗
    open(data, flag) {
      console.log("%c Line:92 🍷 flag", "color:#3f7cff", flag);
      console.log("%c Line:129 🍷 data", "color:#3f7cff", data);
      if (!this.shopInfo) {
        return this.$message.error("非店铺账户不能改分享！");
      }
      this.yunyingshangList = this.$store.getters.gameYunyingshangList;

      if (flag == "edit") {
        // 编辑
        this.form = { ...data };
        this.form.gameCarrierIdsArr = [];
        if (data.gameCarrierIds) {
          const arr = data.gameCarrierIds.split(",");
          if (arr && arr.length) {
            arr.forEach((r) => {
              this.form.gameCarrierIdsArr.push(Number(r));
            });
          }
        }
      } else {
        // 新增
        if (flag == "wozude") {
          this.yunyingshangList = data.carrierTypeVoList;
          this.form.gameCarrierIdsArr = [];
          let gameCarrierNames = [];
          let gameCarrierIds = [];
          if (this.yunyingshangList && this.yunyingshangList.length) {
            this.yunyingshangList.forEach((y) => {
              this.form.gameCarrierIdsArr.push(y.id);
              gameCarrierIds.push(y.id);
              gameCarrierNames.push(y.name);
            });
            this.form.gameCarrierNames = gameCarrierNames.join(",");
            this.form.gameCarrierIds = gameCarrierIds.join(",");
          }
        }
        this.form.tenantId = this.shopInfo.tenantId;
        this.form.tenantName = this.shopInfo.tenantName;

        this.form.tel = data.tel;
      }

      this.dialogVisible = true;
    },
    // 重置
    reset() {
      this.$refs.form.resetFields();
    },
    // 选择运营商
    carrierChange(e) {
      // console.log("%c Line:136 🍅 e", "color:#465975", e);
      let gameCarrierNames = [];
      let gameCarrierIds = [];
      if (e && e.length) {
        e.forEach((r) => {
          this.yunyingshangList.forEach((r2) => {
            if (r2.id == r) {
              // console.log("%c Line:143 🥒 r2", "color:#33a5ff", r2);
              gameCarrierNames.push(r2.name);
              gameCarrierIds.push(r2.id);
            }
          });
        });
      }
      this.form.gameCarrierNames = gameCarrierNames.join(",");
      this.form.gameCarrierIds = gameCarrierIds.join(",");
    },
    async submitForm(formName) {
      this.$refs["" + formName].validate(async (valid) => {
        if (valid) {
          let res = "";
          if (this.form.id) {
            // 编辑
            res = await phoneApi.appMessageShareEdit(this.form);
            this.$parent.getList();
          } else {
            // 新增
            res = await phoneApi.appMessageShare(this.form);
            const Code_url = `${this.retailDomain["h5"].dictValue}pages/SMSList/SMSList/?flag=${res.data.randomStr}`;
            this.$confirm(
              `创建分享成功，链接为:${Code_url}, 在“短信记录>我的分享”菜单可查看`,
              "提示",
              {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
              }
            )
              .then(() => {
                this.$copyText(Code_url).then(() => {
                  this.$message.success("链接已复制到剪切板");
                });
              })
              .catch(() => {
                this.$copyText(Code_url).then(() => {
                  this.$message.success("链接已复制到剪切板");
                });
              });
          }
          this.$message.success(res.msg);
          this.reset();
          this.dialogVisible = false;
        } else {
          this.$alert("请填写完整！");
        }
      });
    },

    close() {
      this.reset();
      this.dialogVisible = false;
    },
  },
};
</script>
<style scoped lang="css">
.center {
  text-align: center;
}
.form-box {
  padding-top: 20px;
}
.tip {
  color: red;
  margin-bottom: 20px;
}
</style>
